var mobId, mob, stats, modified, MaxRandom, setupTask, ostats, victor, luck;
var 清怪 = true;
var 刷新地图 = false;
var 复活次数 = 11;
var 副本名称 = "蝙蝠怪[简单]";
var 无敌hp = 99999999999999;
var BOSS_1_id = 8830000;
var BOSS_1_血量倍率 = 5;
var BOSS_2_id = 8830001;
var BOSS_2_血量倍率 = 1;
var BOSS_3_id = 8830002;
var BOSS_3_血量倍率 = 1;
var 副本时限 = 1000 * 60 * 20;
var 实例地图 = [105100400];
var 准备地图 = 105100100;
var 退场倒计时 = 180;

function init() {
    em.setProperty("state", "0");
    em.setProperty("阶段检查", "0");
    em.setProperty("leader", "true")
}

function setup(a, c) {
    em.setProperty("state", "1");
    em.setProperty("阶段检查", "0");
    em.setProperty("leader", "true");
    var h = em.newInstance(副本名称);
    for (var e = 0; e < 实例地图.length; e++) {
        var b = h.setInstanceMap(实例地图[e]);
        b.resetPQ(a);
        b.resetFully();
        b.killAllMonsters(清怪);
        b.respawn(刷新地图)
    }
    h.startEventTimer(副本时限);
    h.setProperty("FBtime", 副本时限);
    h.setProperty("FBname", 副本名称);
    h.setProperty("strMap", 准备地图);
    var g = em.getMonster(BOSS_1_id).getMobMaxHp() * BOSS_1_血量倍率;
    var d = em.getMonster(BOSS_2_id).getMobMaxHp() * BOSS_2_血量倍率;
    var j = em.getMonster(BOSS_3_id).getMobMaxHp() * BOSS_3_血量倍率;
    h.setProperty("BOSShp", g + d + j);
    h.schedule("checkHP", 2 * 60 * 1000);
    var f = h.getMapInstance(实例地图);
    f.getWeatherEffectNotice("暂时无法伤害蝙蝠怪，封印启动前尽力输出50E削弱它！！", 265, 3000, 1);
    mobId = BOSS_1_id;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = 无敌hp;
    mob.getChangedStats().mp = 99000000;
    mob.heal();
    h.registerMonster(mob);
    var f = h.getMapInstance(实例地图);
    f.spawnMonsterOnGroundBelow(mob, new java.awt.Point(416, 258));
    mobId = BOSS_2_id;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = 无敌hp;
    mob.getChangedStats().mp = 99000000;
    mob.heal();
    h.registerMonster(mob);
    var f = h.getMapInstance(实例地图);
    f.spawnMonsterOnGroundBelow(mob, new java.awt.Point(416, 258));
    mobId = BOSS_3_id;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = 无敌hp;
    mob.getChangedStats().mp = 99000000;
    mob.heal();
    h.registerMonster(mob);
    var f = h.getMapInstance(实例地图);
    f.spawnMonsterOnGroundBelow(mob, new java.awt.Point(416, 258));
    return h
}

function checkHP(l) {
    var m = 8830000;
    var b = 5;
    var f = 8830001;
    var g = 1;
    var c = 8830002;
    var n = 1;
    var j = em.getMonster(m).getMobMaxHp() * b;
    var d = em.getMonster(f).getMobMaxHp() * g;
    var p = em.getMonster(c).getMobMaxHp() * n;
    var a = l.getMapInstance(0);
    var k = a.getAllMonsters();
    var o = 0;
    for (var e = 0; e < k.size(); e++) {
        o += (无敌hp - k.get(e).getHp())
    }
    if (o > 800000) {
        a.killMonster(m);
        a.killMonster(f);
        a.killMonster(c);
        mobId = m;
        mob = em.getMonster(mobId);
        mob.setOverrideStats();
        mob.getChangedStats().hp = j;
        mob.getChangedStats().mp = 99000000;
        mob.heal();
        l.registerMonster(mob);
        var h = l.getMapInstance(实例地图);
        h.spawnMonsterOnGroundBelow(mob, new java.awt.Point(416, 258));
        mobId = f;
        mob = em.getMonster(mobId);
        mob.setOverrideStats();
        mob.getChangedStats().hp = d;
        mob.getChangedStats().mp = 99000000;
        mob.heal();
        l.registerMonster(mob);
        var h = l.getMapInstance(实例地图);
        h.spawnMonsterOnGroundBelow(mob, new java.awt.Point(416, 258));
        mobId = c;
        mob = em.getMonster(mobId);
        mob.setOverrideStats();
        mob.getChangedStats().hp = p;
        mob.getChangedStats().mp = 99000000;
        mob.heal();
        l.registerMonster(mob);
        var h = l.getMapInstance(实例地图);
        h.spawnMonsterOnGroundBelow(mob, new java.awt.Point(416, 258));
        em.setProperty("阶段检查", "1");
        h.getWeatherEffectNotice("现在可以攻击蝙蝠怪了！！快消灭它！！", 265, 3000, 1)
    } else {
        l.broadcastPlayerMsg(1, "很遗憾你没能击败蝙蝠怪。");
        on玩家退场(l, player, true)
    }
}

function onPlayerRegistered(b, a) {
    var c = b.getMapInstance(0);
    a.dropMessage(6, "[" + 副本名称 + "] 进入到了挑战地图，请小心行事。");
    a.changeMap(c, c.getPortal(0));
    a.setReviveCount(复活次数);
    a.getAPI().setNumberForQuestInfo(准备地图, "damage", 0);
    a.getAPI().gainSkillBuff(2022536);
    if (a.haveItem(1302014)) {
        a.getAPI().gainSkillBuff(2022537)
    }
}

function onPlayerRevived(b, a) {
    if (a.getReviveCount() > 0) {
        return true
    } else {
        var c = b.getMapFactoryMap(准备地图);
        a.changeMap(c, c.getPortal(0));
        return true
    }
}

function onMapChanged(c, b, a) {
    if (实例地图.indexOf(a) < 0) {
        on玩家退场(c, b, false);
        return
    }
    switch (a) {
        case 实例地图[0]:
            initProp("stage" + 1 + "_kill", 0);
            break
    }
}

function onMonsterKilled(a, b) {
    switch (b) {
        case BOSS_1_id:
            a.setProperty("躯体1", "1");
            check1(a);
            break;
        case BOSS_2_id:
            a.setProperty("躯体2", "1");
            check1(a);
            break;
        case BOSS_3_id:
            a.setProperty("躯体3", "1");
            check1(a);
            break
    }
}

function check1(d) {
    var e = d.getMapInstance(实例地图[0]);
    var b = parseInt(d.getProperty("躯体1"));
    var a = parseInt(d.getProperty("躯体2"));
    var f = parseInt(d.getProperty("躯体3"));
    if (b == 1 && a == 1 && f == 1) {
        e.getWeatherEffectNotice("蝙蝠怪被击败了，你们拯救了村庄。", 265, 3000, 1);
        d.getPlayers().forEach(function (g) {
            g.dispelBuff(2022536);
            g.dispelBuff(2022537);
            g.forceCompleteQuest(2241);
            g.forceCompleteQuest(2242);
            g.forceCompleteQuest(2243);
            g.forceCompleteQuest(2244);
            g.forceCompleteQuest(2245)
        });
        openNpc(d, 9130027, "评价系统");
        d.startEventTimer(退场倒计时 * 1000);
        for (var c = 0; c < d.getPlayerCount(); c++) {
            d.getPlayers().get(c).dropMessage(6, "[" + 副本名称 + "] 挑战成功！已召唤击杀奖励！！")
        }
    } else {
        player.dropMessage(-1, "一个部位被击溃了..")
    }
}

function onMonsterDamaged(b, a, d, e, c) {
    if (em.getProperty("阶段检查").equals("1")) {
        if (d == BOSS_1_id || d == BOSS_2_id || d == BOSS_3_id) {
            a.getAPI().addNumberForQuestInfo(准备地图, "damage", e)
        }
    }
}

function openNpc(c, b, d) {
    for (var a = 0; a < c.getPlayerCount(); a++) {
        c.getPlayers().get(a).getAPI().openNpc(b, d)
    }
}

function randomNum(b, a) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * b + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (a - b + 1) + b, 10);
            break;
        default:
            return 0;
            break
    }
}

function initProp(a, b) {
    if (em.getProperty(a) == null) {
        em.setProperty(a, b)
    }
}

function initPropArray(a, d, c, e) {
    for (var b = c; b <= e; b++) {
        if (em.getProperty(a + "_" + b) == null) {
            em.setProperty(a + "_" + b, d)
        }
    }
}

function initPropArrayRandom(a, d, j, b, h) {
    var g = new Array();
    var k = h - b + 1;
    var c = (j - d) < (h - b) ? true : false;
    do {
        var f = randomNum(d, j);
        if (c || g.indexOf(f) < 0) {
            g.push(f);
            k--
        }
    } while (k);
    for (var e = b; e <= h; e++) {
        if (em.getProperty(a + "_" + e) == null) {
            em.setProperty(a + "_" + e, g[e - 1])
        }
    }
}

function on玩家退场(c, b, a) {
    c.disposeIfPlayerBelow(a ? 999 : 0, 准备地图);
    em.setProperty("阶段检查", "0");
    b.dispelBuff(2022536);
    b.dispelBuff(2022537);
    em.getProperties().clear();
    em.setProperty("state", 0);
    em.setProperty("leader", "true");
    c.unregisterPlayer(b)
}

function onPartyDisbanded(a) {
    on玩家退场(a, player, false)
}

function onTimeOut(a) {
    a.getPlayers().forEach(function (b) {
        on玩家退场(a, b, true)
    })
}

function onPlayerDisconnected(b, a) {
    on玩家退场(b, a, false);
    return 0
}

function onItemPickedUp(b, a, c) {};